package com.test.cordova.view;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

import com.test.cordova.R;

/**
 * 自定义画板 --> 刮奖版
 */

public class DrawBoard extends View {

    //绘制涂抹图片
    private Paint mPaint;
    private Path mPath;
    private Bitmap mBoard;
    private Canvas mCanvas;

    //遮挡奖图片
    private Bitmap mBG;

    public DrawBoard(Context context) {
        this(context,null);
    }

    public DrawBoard(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs,0);
    }

    public DrawBoard(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initPaint();
    }

    private void initPaint() {
        mPaint = new Paint();
        mPath = new Path();
        //奖品背景
        mBG = BitmapFactory.decodeResource(getResources(), R.drawable.screen);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        //必须保留 否则下面获取不到测量高度和宽度
        super.onMeasure(widthMeasureSpec,heightMeasureSpec);

        int width = MeasureSpec.getSize(widthMeasureSpec);
        int height = MeasureSpec.getSize(heightMeasureSpec);
        int measuredHeight = getMeasuredHeight();
        int measuredWidth = getMeasuredWidth();

        //画板背景
        mBoard = Bitmap.createBitmap(measuredWidth,measuredHeight, Bitmap.Config.ARGB_8888);
        mCanvas = new Canvas(mBoard);
        setPaint();

        mCanvas.drawColor(Color.parseColor("#c0c0c0"));
    }

    /**
     * 设置画笔属性
     */
    private void setPaint() {
        mPaint.setColor(Color.RED);
        mPaint.setAntiAlias(true);
        mPaint.setDither(true);
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setStrokeWidth(12);
        mPaint.setStrokeJoin(Paint.Join.ROUND);
        mPaint.setStrokeCap(Paint.Cap.ROUND);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        canvas.drawBitmap(mBG,0,0,null);
        drawPath();
        canvas.drawBitmap(mBoard,0,0,null);
    }

    private void drawPath() {
        mPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_OUT));
        mCanvas.drawPath(mPath,mPaint);
    }

    private int mCurrentX;
    private int mCurrentY;

    @Override
    public boolean onTouchEvent(MotionEvent event) {

        int action = event.getAction();
        int x = (int) event.getX();
        int y = (int) event.getY();

        switch (action) {
            case MotionEvent.ACTION_DOWN:
                 mCurrentX = x;
                 mCurrentY = y;
                 mPath.moveTo(mCurrentX,mCurrentY);
                break;
            case MotionEvent.ACTION_MOVE:
                int dx = Math.abs(x-mCurrentX);
                int dy = Math.abs(y-mCurrentY);
                if (dx > 3 || dy > 3) {
                    mPath.lineTo(x,y);
                }
                mCurrentX = x;
                mCurrentY = y;
                break;
            case MotionEvent.ACTION_UP:
                break;
        }
        invalidate();
        return true;
    }
}
